<?php
date_default_timezone_set('Asia/Shanghai');
header('Content-Type:text/javascript');
header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR";');
if(isset($_SERVER['HTTP_CLIENT_IP'])){$ip=$_SERVER['HTTP_CLIENT_IP'];}elseif(isset($_SERVER['HTTP_X_FORWARDED_FOR'])){$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];}else{$ip=$_SERVER['REMOTE_ADDR'];}
if($_COOKIE['visitor']){$visitor=$_COOKIE['visitor'];}else{$visitor=strtoupper(md5($ip.$_GET['G'].uniqid()));setcookie('visitor',$visitor,9999999999,'/','',false,false);}
$track=substr(str_shuffle(mt_rand(0,9999999999).'ABCDEF'),0,8);$board=explode(',',$_GET['M']);
/***********************采集用户端发送来数据******************************/
$data=array($_SERVER['REQUEST_TIME'],$visitor,$track,$_GET['I'],$board[0],$board[1],$board[2],$board[3]);
/***********************取得当前IP地址ID******************************/
$city=0;$ip=ip2long($ip);$find=pack('N',$ip);$china=apc_fetch('china');
$front=0;$count=strlen($china)/6;
while($front<$count-1){
	$i=ceil(($front+$count)/2);$start=strcmp($find,substr($china,$i*6,4));
	if($start<0){$count=$i;}else{
		$end=strcmp($find,substr($china,($i+1)*6,4));
		if($end>=0){
			$front=$i;
		}else{
			$city=unpack('C2',substr($china,$i*6+4,2));
			$city=$city[1]*256+$city[2];
			break;
		}
	}
}
/***********************智能获取当前用户符合广告******************************/
if(apc_fetch('advertisers')===1){
	$ad=apc_fetch($board[1].':'.$board[3]);
	if(empty($ad)){
		echo 'alert("广告位对应广告为空.");';
	}else{
		$ad=unserialize($ad);
		if(!empty($_GET['H'])){
			$_GET['H']=urldecode($_GET['H']);
			if(1==preg_match('/^([\x09\x0A\x0D\x20-\x7E]|[\xC2-\xDF][\x80-\xBF]|\xE0[\xA0-\xBF][\x80-\xBF]|[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}|\xED[\x80-\x9F][\x80-\xBF]|\xF0[\x90-\xBF][\x80-\xBF]{2}|[\xF1-\xF3][\x80-\xBF]{3}|\xF4[\x80-\x8F][\x80-\xBF]{2})*\z/x',$_GET['H'])){
				$_GET['H']=iconv('gb2312','UTF-8',$_GET['H']);
			}
			$user[]='(\d+):'.$_GET['H'];
		}
		if(count($ad)>1){//智能投放
			$user[]='(\d+):'.$city;$adcolumn=explode('-',$board[4]);
			foreach($adcolumn as $value){$user[]='(\d+):'.$value;}
			$user=implode('|',$user);$aptitude=array();
			foreach($ad as $key=>$value){
				$matches=array();$aptitude[$key]=0;
				preg_match_all('/'.$user.'/u',$value['tactic'],$matches,PREG_PATTERN_ORDER);
				if(isset($matches[0])){
					foreach($matches[0] as $v){
						$aptitude[$key]+=intval($v);
					}
				}
			}
			array_multisort($aptitude,SORT_DESC,$ad);
		}
		$ad=reset($ad);$script=$ad['script'];
		echo $script['func']."('{$script['size']}','{$script['format']}','{$script['material']}','http://i.cnadlm.com/apc.track.php?url={$ad['link']}&action=click&track={$track}&pt={$_SERVER['REQUEST_TIME']}','{$_GET['V']}');";
		$data[5]=$ad['format'];
		$data[]=$ad['id'];$data[]=0;$data[]=$city;$data[]=$_GET['C'];$data[]=$_GET['D'];$data[]=(empty($_GET['E'])?$_SERVER['HTTP_ACCEPT_LANGUAGE']:$_GET['E']);$data[]=$_GET['F'];$data[]=$_GET['G'];$data[]=$_GET['H'];$data[]=$board[4];$data[]=$_GET['V'];$data[]=$board[1];
		$url='http://127.0.0.1:1218/?name=pageview&opt=put&data='.str_replace('&','',implode(',',$data)).'&auth=cloud';
		@file_get_contents($url,0,stream_context_create(array('http'=>array('method'=>'GET','timeout'=>1))));
	}
}else{
	echo 'alert("系统正在更新广告数据中.");';
}
?>